import Vue from 'vue';
import Router from 'vue-router';
import store from '../vuex/store';
Vue.use(Router);
const router = new Router({
  routes: [
    { path: "/", redirect: "/home" },
    {
      path: '/main',component: resolve => require(['../views/layout/index'], resolve),
      children: [
        { path: '/home', component: resolve => require(['../views/home'], resolve), meta: [{ name: "首页" }] },
        { path: '/refresh', component: resolve => require(['../views/home/refresh'], resolve),meta:[{name:""}]},
      ]
    },
    { path: '*', component: resolve => require(['../views/error'], resolve) },
    { path: '/login', component: resolve => require(['../views/login/login'], resolve) },
  ]
})
/* 路由拦截 */
router.beforeEach(function (to, from, next) {
  let token = sessionStorage.getItem('token');
  if (token != '' && token != undefined && token != 'null') {
    next();
  } else {
    if (to.path === '/login') {
      next();
    } else {
      next('/login');
    }
  }
})
router.afterEach((to, from, next) => {
  var routerList = to.matched;
  store.commit('setCrumbList', routerList)
})

export default router;
